<?php
/* 
 * This model handles all information regarding emails.
 * And sending of new passwords or other emails.
 */

/**
 * Description of email_model
 *
 * @author ChristianNotebook
 */
class Model_email extends CI_Model
{
    function  __construct() 
    {
        parent::__construct();
    }

    // function that takes an int of long the random string needs to be
    // and returns a random string with the given length.
    // Found on the internet on the php website comment about random generator
    function generateRandStr($length)
    {
        $randstr = "";
        for($i=0; $i < $length; $i++)
        {
            $randnum = mt_rand(0,61);
            if ($randnum < 10 )
            {
                $randstr.=chr($randnum+48);
            }
            else if ($randnum < 36)
            {
                $randstr .= chr($randnum + 55);
            }
            else
            {
                $randstr .= chr($randnum + 61);
            }
        }
        return $randstr;
    }

    // returns a new random password 8 char long
    function newPasswordStr()
    {
        $newPass = $this->generateRandStr(8);
        $newPassStr = $newPass;
        return $newPassStr;
    }

    // checks if an email is in the db and returns the id of the emails user.
    function emailValidation()
    {
        $validemail = $this->input->post('email');
        $this->db->where('email', $validemail);
        $Q = $this->db->get('user');
        if($Q->num_rows == 1)
        {
            $row = $Q->row_array();
            if ($row['email'] == $validemail)
            {
                $id = $row['id'];
                return $id;
            }
        }
        return false;
    }

    // updates the password to a user given by the id 
    function updatePassword($id, $newpass) {
        error_log($newpass);
        $data = array(
            'password' => md5($newpass)
            );
        $this->db->where('id', $id);
        $this->db->update('user', $data);
    }

}
?>
